﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;

namespace PTTPOS_Solution.DAL
{
    public static class Orderdetails_pro_DA
    {
        public static DataTable ListOrderdetails = new DataTable();
        public static void GetListOrderdetails_pro(string ID_order)
        {
            MySqlCommand com = new MySqlCommand("select Rebateform, Value, tblmenus.ID, tblorderdetails_pro.Price, tblorderdetails_pro.Price2, Amount, Name from tblorderdetails_pro inner join tblmenus on tblorderdetails_pro.ID_menu=tblmenus.ID where tblorderdetails_pro.ID_order=@ID_order", Connection_PTTPOS.GetConnection());
            com.Connection.Open();
            com.Parameters.AddWithValue("@ID_order", ID_order);
            MySqlDataAdapter da = new MySqlDataAdapter(com);
            ListOrderdetails.Clear();
            da.Fill(ListOrderdetails);
            com.Connection.Close();
        }

        public static void InsertOrderdetails_pro(string id_order, string id_menu, decimal Price, decimal Price2, string Amount)
        {
            MySqlCommand com = new MySqlCommand("insert into tblorderdetails_pro(id, id_order, id_menu, Price, Price2, Amount) values(null, @id_order, @id_menu, @Price, @Price2, @Amount)", Connection_PTTPOS.GetConnection());
            com.Parameters.AddWithValue("@id_order", id_order);
            com.Parameters.AddWithValue("@id_menu", id_menu);
            com.Parameters.AddWithValue("@Price", Price);
            com.Parameters.AddWithValue("@Price2", Price2);
            com.Parameters.AddWithValue("@Amount", Amount);
            com.Connection.Open();
            com.ExecuteNonQuery();
            com.Connection.Close();
        }

        public static void UpdateOrderdetails_pro_Price(string id_order, string id_menu, decimal Price)
        {
            MySqlCommand com = new MySqlCommand("update tblorderdetails_pro set Price2= @Price2 where id_order=@id_order and id_menu=@id_menu", Connection_PTTPOS.GetConnection());
            com.Parameters.AddWithValue("@id_order", id_order);
            com.Parameters.AddWithValue("@id_menu", id_menu);
            com.Parameters.AddWithValue("@Price2", Price);
            com.Connection.Open();
            com.ExecuteNonQuery();
            com.Connection.Close();
        }

        public static void UpdateOrderdetails_pro_Amout(string id_order, string id_menu, string Amount)
        {
            MySqlCommand com = new MySqlCommand("update tblorderdetails_pro set Amount= @Amount where id_order=@id_order and id_menu=@id_menu", Connection_PTTPOS.GetConnection());
            com.Parameters.AddWithValue("@id_order", id_order);
            com.Parameters.AddWithValue("@id_menu", id_menu);
            com.Parameters.AddWithValue("@Amount", Amount);
            com.Connection.Open();
            com.ExecuteNonQuery();
            com.Connection.Close();
        }

        public static void DeleteProducts_pro(string id_order, string id_menu) // Phương thức hủy món
        {
            MySqlCommand command = new MySqlCommand("Delete from tblorderdetails_pro where id_order=@id_order and id_menu=@id_menu", Connection_PTTPOS.GetConnection());
            command.Connection.Open();
            command.Parameters.AddWithValue("@id_order", id_order);
            command.Parameters.AddWithValue("@id_menu", id_menu);
            command.ExecuteNonQuery();
            command.Connection.Close();
        }

        public static void CompileTableorderdetail_pro(string id_orderFrom, string id_orderTo) // phương thức sửa mã order sử dụng cho việc gộp bàn.
        {
            getPriceOfMenu_pro(id_orderFrom);
            MySqlCommand command = new MySqlCommand("update tblorderdetails_pro set id_order=@id1 where id_order=@id2", Connection_PTTPOS.GetConnection());
            command.Parameters.AddWithValue("@id1", id_orderFrom);
            command.Parameters.AddWithValue("@id2", id_orderTo);
            command.Connection.Open();
            command.ExecuteNonQuery();
            command.Connection.Close();
            getupdate_pro(id_orderFrom);

            for (int i = 0; i < tblupdate.Rows.Count; i++)
            {
                for (int j = i + 1; j < tblupdate.Rows.Count; j++)
                {
                    if (tblupdate.Rows[i][2].ToString() == tblupdate.Rows[j][2].ToString())
                    {
                        for (int k = 0; k < tblPriceOfMenu.Rows.Count; k++)
                        {
                            if (tblupdate.Rows[i][2].ToString() == tblPriceOfMenu.Rows[k][0].ToString())
                            {
                                update_pro(tblupdate.Rows[i][0].ToString(), (Convert.ToInt16(tblupdate.Rows[i][5]) + Convert.ToInt16(tblupdate.Rows[j][5])).ToString(), tblPriceOfMenu.Rows[k][1].ToString());
                                delete_pro(tblupdate.Rows[j][0].ToString());
                            }
                        }
                    }
                }
            }
        }
        public static DataTable tblupdate = new DataTable();
        public static void getupdate_pro(string id_order)
        {
            MySqlCommand com = new MySqlCommand("select * from tblorderdetails_pro where id_order=@id_order", Connection_PTTPOS.GetConnection());
            com.Connection.Open();
            com.Parameters.AddWithValue("@id_order", id_order);
            MySqlDataAdapter da = new MySqlDataAdapter(com);
            tblupdate.Clear();
            da.Fill(tblupdate);
            com.Connection.Close();
        }
        public static DataTable tblPriceOfMenu = new DataTable();
        public static void getPriceOfMenu_pro(string id_order)
        {
            MySqlCommand com = new MySqlCommand("select id_menu,price2 from tblorderdetails_pro where id_order=@id_order", Connection_PTTPOS.GetConnection());
            com.Connection.Open();
            com.Parameters.AddWithValue("@id_order", id_order);
            MySqlDataAdapter da = new MySqlDataAdapter(com);
            tblPriceOfMenu.Clear();
            da.Fill(tblPriceOfMenu);
            com.Connection.Close();
        }
        public static void update_pro(string id_order, string amount, string price2)
        {
            MySqlCommand command2 = new MySqlCommand("update tblorderdetails_pro set amount=@amount, price2=@price2 where id=@id_order", Connection_PTTPOS.GetConnection());
            command2.Connection.Open();
            command2.Parameters.AddWithValue("@amount", amount);
            command2.Parameters.AddWithValue("@id_order", id_order);
            command2.Parameters.AddWithValue("@price2", price2);
            command2.ExecuteNonQuery();
            command2.Connection.Close();
        }
        public static void delete_pro(string id)
        {
            MySqlCommand command2 = new MySqlCommand("Delete from tblorderdetails_pro where id=@id", Connection_PTTPOS.GetConnection());
            command2.Connection.Open();
            command2.Parameters.AddWithValue("@id", id);
            command2.ExecuteNonQuery();
            command2.Connection.Close();
        }

        public static void delete_pro1(string id_order)
        {
            MySqlCommand command2 = new MySqlCommand("Delete from tblorderdetails_pro where id_order=@id_order", Connection_PTTPOS.GetConnection());
            command2.Connection.Open();
            command2.Parameters.AddWithValue("@id_order", id_order);
            command2.ExecuteNonQuery();
            command2.Connection.Close();
        }
        public static void update_pro1(string id_order, List<string> lstID_Menu)
        {
            foreach (string stridMenu in lstID_Menu)
            {
                MySqlCommand command2=null;
                if (stridMenu.Split('|')[2] == "true")
                {
                    command2 = new MySqlCommand("Update tblorderdetails_pro set amount=amount-@amount where id_order=@id_order and id_menu = @idMenu", Connection_PTTPOS.GetConnection());
                    command2.Connection.Open();
                    command2.Parameters.AddWithValue("@id_order", id_order);
                    command2.Parameters.AddWithValue("@idMenu", stridMenu.Split('|')[0]);
                    command2.Parameters.Add("@amount", MySqlDbType.Int32).Value = stridMenu.Split('|')[1];
                }
                else if (stridMenu.Split('|')[2] == "false")
                {
                    command2 = new MySqlCommand("Delete from tblorderdetails_pro where id_order=@id_order and id_menu = @idMenu", Connection_PTTPOS.GetConnection());
                    command2.Connection.Open();
                    command2.Parameters.AddWithValue("@id_order", id_order);
                    command2.Parameters.AddWithValue("@idMenu", stridMenu.Split('|')[0]);
                }
                command2.ExecuteNonQuery();
                command2.Connection.Close();
            }
        }

        public static void update_pro_amount(string id_order, string id_menu, int amount)
        {
            MySqlCommand command2 = new MySqlCommand("update tblorderdetails_pro set amount=amount - @amount where id_order=@id_order and id_menu=@id_menu", Connection_PTTPOS.GetConnection());
            command2.Connection.Open();
            command2.Parameters.AddWithValue("@amount", amount);
            command2.Parameters.AddWithValue("@id_order", id_order);
            command2.Parameters.AddWithValue("@id_menu", id_menu);
            command2.ExecuteNonQuery();
            command2.Connection.Close();
        }
    }
}
